#!/usr/bin/env ruby
require 'net/http'
require 'net/https'
require 'digest/sha1'
require 'base64'
require 'time'
require 'digest'
require 'openssl'
require 'uri'
require 'cgi'
require 'json'


#access_key_id = 'QYACCESSKEYIDEXAMPLE'
#secret_access_key = 'SECRETACCESSKEY'

access_key_id = 'UFWXJSFBPZKJZLZPKCBI'
secret_access_key = 'DQSEu9Ey7xTQb8nVIHpxvvk6xk1JGfTamsfKgU5z'

t = Time.now

format='%Y-%m-%dT%H:%M:%SZ'

timestamp= t.strftime(format).to_s

encodeTimeStamp=CGI.escape(timestamp).strip()

puts encodeTimeStamp

qureyStr_getDec='access_key_id=UFWXJSFBPZKJZLZPKCBI&action=DescribeInstances&&instances.n=j-0pjx2qd8&signature_method=HmacSHA256&signature_version=1&time_stamp='+encodeTimeStamp+'&version=1&vxnets.1=vxnet-jg716a6&zone=pek2'

signare_GetDec_Str='GET\n/iaas/\n'+qureyStr_getDec

afterHMAC_getDec=OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret_access_key, signare_GetDec_Str)

after_base64_getDec= Base64.encode64(afterHMAC_getDec).strip()

signatured_getDec_Str= CGI.escape(after_base64_getDec)

finalURL='https://api.qingcloud.com/iaas/?'+qureyStr_getDec+'&signature='+signatured_getDec_Str

puts "finalURL is: "+finalURL

uri=URI.parse(finalURL.strip)

http = Net::HTTP.new(uri.host, uri.port)

http.use_ssl = true

http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Get.new(uri.request_uri)

response = http.request(request)

puts response.body




